package com.zhenyi.api.file;

import java.io.File;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;

/**
 * 文档地址: https://developer.qiniu.com/kodo/sdk/1239/java
 * 
 * @author Frank Cen at home2019年1月14日 下午11:13:25
 *
 */
public class QNUtils {

	private String host;
	private String bucket;
	private String accessKey;
	private String secretKey;
	private Auth auth;

	/**
	 * 
	 * @param host
	 *            http://res.zhen-yee.com
	 * @param bucket
	 * @param accessKey
	 * @param secretKey
	 */
	public QNUtils(String host, String bucket, String accessKey, String secretKey) {
		this.bucket = bucket;
		this.host = host;
		this.accessKey = accessKey;
		this.secretKey = secretKey;
		auth = Auth.create(accessKey, secretKey);
	}

	public QNUtils(String bucket, String accessKey, String secretKey) {
		this.bucket = bucket;
		this.accessKey = accessKey;
		this.secretKey = secretKey;
		auth = Auth.create(accessKey, secretKey);
	}

	/**
	 * Js上传token
	 * 
	 * @return
	 * @Time 2019年1月14日 下午11:13:03
	 */
	public String getToken() {
		return auth.uploadToken(bucket);
	}

	/**
	 * 上传且返回完整路径
	 * 
	 * @param file
	 *            上传文件
	 * @param project
	 *            上传前缀
	 * @param key
	 *            http://domain/project/date/key
	 * @return
	 * @throws QiniuException
	 * @Time 2019年1月14日 下午11:16:21
	 */
	public String uploadWithPrefix(File file, String project, String key) throws QiniuException {
		if (StrKit.isBlank(host)) {
			return "";
		}
		if (StrKit.isBlank(project)) {
			return "";
		}
		if (StrKit.isBlank(key)) {
			key = System.currentTimeMillis() + RandomUtil.randomString(4);
		}
		String url = project + "/" + DateUtil.today() + "/" + key;
		String responseKey = upload(file, url);
		if (StrKit.isBlank(responseKey)) {
			return "";
		}
		return host + "/" + upload(file, url);
	}

	/**
	 * 删除文件
	 * 
	 * @param key
	 *            完整key路径
	 * @return
	 * @Time 2019年1月14日 下午11:15:41
	 */
	public boolean remove(String key) {
		BucketManager bucketManager = new BucketManager(auth);
		try {
			bucketManager.delete(bucket, key);
			return true;
		} catch (QiniuException ex) {
			// 如果遇到异常，说明删除失败
			System.err.println(ex.code());
			System.err.println(ex.response.toString());
			return false;
		}
	}

	/**
	 * 上传
	 * 
	 * @param file
	 * @param key
	 *            如果key未空,则为当前时间戳+4位随机数
	 * @return
	 * @throws QiniuException
	 * @Time 2019年1月14日 下午11:15:54
	 */
	public String upload(File file, String key) throws QiniuException {
		if (!FileUtil.exist(file)) {
			LogKit.warn("no file");
			return "";
		}
		Auth auth = Auth.create(accessKey, secretKey);
		String token = auth.uploadToken(bucket);
		UploadManager uploadManager = new UploadManager();
		String path = file.getAbsolutePath();
		if (StrKit.isBlank(key)) {
			key = System.currentTimeMillis() + "";
		}
		Response res = uploadManager.put(path, key, token);
		if (!res.isOK()) {
			LogKit.warn("upload qn error " + res.bodyString());
			return "";
		}
		return key;
	}
}
